Skip to content

Evaluate try_emplace arguments before checking the key - #583

Merged
nunoplopes merged 1 commit into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-pkav2pt2td
Nov 16, 2026
Merged

nunoplopes merged 1 commit into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-pkav2pt2td

Conversation

@perfloop-agent

@perfloop-agent perfloop-agent commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Evaluate std::map::try_emplace(key, args...) arguments before the key lookup, even when the key already exists, while keeping mapped-object construction conditional on insertion. This fixes skipped argument side effects for lvalue and const keys in both refcount and unsafe output.

The rules IR init fragment gains an optional conditional flag, detected from its position in the rule body. The converter uses it to bind argument effects before expanding the rule body without eagerly constructing the stored value. Reference binding and std::move forwarding are preserved: arguments are not copied or moved from on a key hit, and insertion retains the C++ copy/move behavior.

The binding also works in global initializers and with assignment, compound assignment, prefix increment, comma and conditional expressions. Modified lvalue arguments remain addressable without repeating their operand effects: a statement visitor finds their address uses, and an assignment used as a reference is lowered to Rust bindings rather than new AST nodes.

In unsafe output, try_emplace now lowers to try_emplace(key, || value) instead of emplace((key, value)), so tests/unit/out/unsafe/map.rs changes in three places.

Testing

The two new unit tests, tests/unit/map_try_emplace.cpp and tests/unit/map_try_emplace_lvalues.cpp, run in both output models. They check argument evaluation, conditional construction, reference binding, copy/move behavior, global initialization and modified lvalue arguments against native C++. The existing-key increment case fails without the fix in both models.

Rebased on master (5eeb075). ninja check in Release passes with the change: lit reports 615 passed, 13 expected failures and no unexpected failures; the unpatched base reports 613 passed, 13 expected failures and no unexpected failures. The runtime, compile-fail and control-flow checks pass on both.

The changed C++ files pass clang-format. The rule preprocessor passes rustfmt and Clippy with warnings denied.

Coverage does not establish the rvalue-key overload or exception unwinding.

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart. To ask for a change, comment or review in plain words. How Perfloop handles pull requests.

@@ -283,6 +285,29 @@ class AddressTakenVisitor
}
}

// A bound lvalue must stay addressable even when its evaluation also writes
// it, as in ++counter or counter = value.
void AddAddressUse(const clang::Expr *expr) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should use a visitor

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: the address-use check is now AddressUseVisitor, a clang::ConstStmtVisitor in boxed_vars.cpp, instead of the hand-written recursion.

@@ -2729,6 +2721,52 @@ bool Converter::VisitBinaryOperator(clang::BinaryOperator *expr) {
return false;
}

void Converter::ConvertAssignmentRef(clang::BinaryOperator *expr) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function doesn't follow Cpp2Rust's style. We try to avoid creating new AST nodes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: ConvertAssignmentRef no longer creates AST nodes. It evaluates the right operand and the target pointer into Rust bindings, writes through the pointer, and returns the same place (match ({ rhs }, ptr) { (r, p) => { p.write(r); p } }).

Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Tomás Senart <ts@perfloop.ai>
@perfloop-agent
perfloop-agent force-pushed the perfloop-pr-open-pkav2pt2td branch from def575b to b060986 Compare November 16, 2026 07:33
@nunoplopes
nunoplopes merged commit 5b1ebf7 into Cpp2Rust:master Nov 16, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants